package com.sskj.fca.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.fca.R;
import com.sskj.fca.bean.CommonBean;
import com.sskj.fca.presenter.HelpCenterFragmentPresenter;
import com.sskj.fca.ui.adapter.ExpandAdapter;
import com.sskj.fca.ui.adapter.Level0Item;
import com.sskj.fca.ui.adapter.Level1Item;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.APP_FRAGMENT_HELP_CENTER)//帮助中心
public class HelpCenterFragment extends BaseFragment<HelpCenterFragmentPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @Autowired
    String position;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private ExpandAdapter expandAdapter;
    private ArrayList<MultiItemEntity> list = new ArrayList<>();


    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_help_center;
    }

    @Override
    public HelpCenterFragmentPresenter getPresenter() {
        return new HelpCenterFragmentPresenter();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mPresenter.getCommonData();
        }
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        mPresenter.getCommonData();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        expandAdapter = new ExpandAdapter(list);
        recyclerView.setAdapter(expandAdapter);
    }

    public void updataUI(CommonBean commonBean) {
        list.clear();
        Level0Item level0Item = new Level0Item("用户协议");
        Level0Item level0Item1 = new Level0Item("隐私政策");
        Level0Item level0Item2 = new Level0Item("常见问题");
        Level0Item level0Item3 = new Level0Item("手续费");
        Level0Item level0Item4 = new Level0Item("忘记密码");
        Level0Item level0Item5 = new Level0Item("注册和登录");
        Level0Item level0Item6 = new Level0Item("技术问题");
        Level0Item level0Item7 = new Level0Item("联系我们");
        CommonBean.HelpInfoBean help_info = commonBean.getHelp_info();
        List<CommonBean.HelpInfoBean.RegAgreeBean> reg_agree = help_info.getReg_agree();
        for (int i = 0; i < reg_agree.size(); i++) {
            Level1Item level1Item = new Level1Item(reg_agree.get(i).getTitle(), reg_agree.get(i).getContent().replaceAll("\\|", "\n"));
            level0Item.addSubItem(level1Item);
        }
        List<CommonBean.HelpInfoBean.WalletRuleBean> wallet_rule = help_info.getWallet_rule();
        for (int i = 0; i < wallet_rule.size(); i++) {
            Level1Item level1Item = new Level1Item(wallet_rule.get(i).getTitle(), wallet_rule.get(i).getContent().replaceAll("\\|", "\n"));
            level0Item1.addSubItem(level1Item);
        }
        List<CommonBean.HelpInfoBean.ProblemBean> problem = help_info.getProblem();
        for (int i = 0; i < problem.size(); i++) {
            Level1Item level1Item = new Level1Item(problem.get(i).getTitle(), problem.get(i).getContent().replaceAll("\\|", "\n"));
            level0Item2.addSubItem(level1Item);
        }
        List<CommonBean.HelpInfoBean.CostBean> cost = help_info.getCost();
        for (int i = 0; i < cost.size(); i++) {
            Level1Item level1Item = new Level1Item(cost.get(i).getTitle(), cost.get(i).getContent().replaceAll("\\|", "\n"));
            level0Item3.addSubItem(level1Item);
        }
        List<CommonBean.HelpInfoBean.PasswordBean> password = help_info.getPassword();
        for (int i = 0; i < password.size(); i++) {
            Level1Item level1Item = new Level1Item(password.get(i).getTitle(), password.get(i).getContent().replaceAll("\\|", "\n"));
            level0Item4.addSubItem(level1Item);
        }
        List<CommonBean.HelpInfoBean.LoginBean> login = help_info.getLogin();
        for (int i = 0; i < login.size(); i++) {
            Level1Item level1Item = new Level1Item(login.get(i).getTitle(), login.get(i).getContent().replaceAll("\\|", "\n"));
            level0Item5.addSubItem(level1Item);
        }
        List<CommonBean.HelpInfoBean.StraitBean> strait = help_info.getStrait();
        for (int i = 0; i < strait.size(); i++) {
            Level1Item level1Item = new Level1Item(strait.get(i).getTitle(), strait.get(i).getContent().replaceAll("\\|", "\n"));
            level0Item6.addSubItem(level1Item);
        }
        List<CommonBean.HelpInfoBean.ContactBean> contact = help_info.getContact();
        for (int i = 0; i < contact.size(); i++) {
            Level1Item level1Item = new Level1Item(contact.get(i).getTitle(), contact.get(i).getContent().replaceAll("\\|", "\n"));
            level0Item7.addSubItem(level1Item);
        }
        list.add(level0Item);
        list.add(level0Item1);
        list.add(level0Item2);
        list.add(level0Item3);
        list.add(level0Item4);
        list.add(level0Item5);
        list.add(level0Item6);
        list.add(level0Item7);

        expandAdapter.notifyDataSetChanged();
        if (!TextUtils.isEmpty(position)) {
            expandAdapter.expand(Integer.valueOf(position));
        }
    }
}
